/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import bean.business.classBean;
import entities.Classes;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author HoangHai
 */
@ManagedBean
@RequestScoped
public class manageClass {

    @EJB
    private classBean classBean;
    private List<Classes> list;
    private Classes c = new Classes();
    private Classes c1 = new Classes();

    public Classes getC1() {
        return c1;
    }

    public void setC1(Classes c1) {
        this.c1 = c1;
    }

    public Classes getC() {
        return c;
    }

    public void setC(Classes c) {
        this.c = c;
    }

    public List<Classes> getList() {
        list = classBean.allClasses();
        return list;
    }

    public void setList(List<Classes> list) {
        this.list = list;
    }

    /** Creates a new instance of manageClass */
    public manageClass() {
    }

    public void insert() {
        if (checkName()) {
            classBean.addClass(c1);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Success"));
        }
    }

    public void update() {
        classBean.updateClass(c);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Success"));
    }

    public void delete() {
        classBean.delClass(c.getClassID());
    }

    public boolean checkName() {
        List<Classes> cs = new LinkedList<Classes>();
        cs = classBean.allClasses();
        for (Classes classes : cs) {
            if (classes.getClassName().equals(c1.getClassName())) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Name had been already !"));
                return false;
            }
        }
        return true;
    }
}
